import 'package:flutter/material.dart';
import '../../../../data/models/directory.dart' as dir;
import '../../../../utils/formatters.dart';

/// 目录项组件，用于在文件浏览器中显示单个目录
class DirectoryItem extends StatelessWidget {
  /// 目录数据
  final dir.Directory directory;

  /// 点击回调
  final VoidCallback onTap;

  /// 菜单操作回调
  final Function(String, dir.Directory) onMenuAction;

  const DirectoryItem({
    Key? key,
    required this.directory,
    required this.onTap,
    required this.onMenuAction,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: Container(
        width: 40,
        height: 40,
        decoration: BoxDecoration(
          color: Theme.of(context).colorScheme.primaryContainer,
          borderRadius: BorderRadius.circular(8),
        ),
        child: const Icon(Icons.folder),
      ),
      title: Text(directory.name),
      subtitle: Text(
        directory.fileCount != null
            ? '${directory.fileCount} 个项目 • ${Formatters.formatDate(directory.updateTime)}'
            : Formatters.formatDate(directory.updateTime),
      ),
      onTap: onTap,
      trailing: _buildMenuButton(),
    );
  }

  /// 构建菜单按钮
  Widget _buildMenuButton() {
    return PopupMenuButton<String>(
      icon: const Icon(Icons.more_vert),
      onSelected: (value) => onMenuAction(value, directory),
      itemBuilder: (context) {
        return [
          const PopupMenuItem<String>(
            value: 'rename',
            child: Row(
              children: [
                Icon(Icons.edit, size: 20),
                SizedBox(width: 8),
                Text('重命名'),
              ],
            ),
          ),
          const PopupMenuItem<String>(
            value: 'share',
            child: Row(
              children: [
                Icon(Icons.share, size: 20),
                SizedBox(width: 8),
                Text('分享'),
              ],
            ),
          ),
          const PopupMenuItem<String>(
            value: 'delete',
            child: Row(
              children: [
                Icon(Icons.delete, size: 20),
                SizedBox(width: 8),
                Text('删除'),
              ],
            ),
          ),
        ];
      },
    );
  }
}
